package com.worldcup.controller.question;

import com.worldcup.common.CommonResult;
import com.worldcup.model.User;
import com.worldcup.model.dto.question.MatchResultDetailDTO;
import com.worldcup.model.dto.question.MatchResultFriendRankDTO;
import com.worldcup.model.question.QuestionMatchResult;
import com.worldcup.service.UserService;
import com.worldcup.service.question.QuestionMatchResultService;
import com.worldcup.service.question.UserFriendService;
import com.worldcup.service.relation.UserRelationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author duwan.zq
 * @date 2019/04/04
 */
@RestController
@RequestMapping("/api/java/question/match/")
public class MatchResultController {

    @Autowired
    private QuestionMatchResultService matchResultService;

    @Autowired
    private UserService userService;

    @Autowired
    private UserFriendService friendService;

    @RequestMapping("/result")
    public CommonResult getMatchResult(String openid, String friendOpenid){
        QuestionMatchResult matchResult = matchResultService.getMatchResult(openid, friendOpenid);
        User user = userService.getUser(openid);
        User friend = userService.getUser(friendOpenid);
        return CommonResult.buildSuccess(new MatchResultDetailDTO(matchResult, user, friend));
    }

    @RequestMapping("/friendRank")
    public CommonResult getFriendRand(String openid){
        List<String> friendOpenids = friendService.getFriendIds(openid);
        if(CollectionUtils.isEmpty(friendOpenids)){
            return CommonResult.buildSuccess(Collections.EMPTY_LIST);
        }
        List<QuestionMatchResult> matchResults = matchResultService.getFriendRank(openid, friendOpenids);
        //todo 百分比
        List<MatchResultFriendRankDTO> result = matchResults.stream().map(matchResult -> {
            User friend = userService.getUser(matchResult.getPassiveOpenid());
            return new MatchResultFriendRankDTO(matchResult, friend);
        }).collect(Collectors.toList());
        return CommonResult.buildSuccess(result);
    }

}
